package com.zzx.threekingdom.view;

import com.zzx.threekingdom.R;
import com.zzx.threekingdom.card.Card;
import com.zzx.threekingdom.card.CardList;
import com.zzx.threekingdom.view.CardPickerView.onSelectCardListener;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.view.Window;
import android.widget.TextView;

import java.util.ArrayList;

public class PickCardDialog extends Dialog implements CardPickerView.onSelectCardListener
{
    public ArrayList<ArrayList<Card>> CardsList = new ArrayList<ArrayList<Card>>();
    public ArrayList<Card> cardList = new ArrayList<Card>();
    
    public TextView mHintTextView;
    public CardPickerView mCardPicker;
    public CardPickerView mCardPicker2;
    public CardPickerView mCardPicker3;

    public onSelectCardListener mListener = null;
    
    public boolean bGotCard = false;
    
    public PickCardDialog(Context context)
    {
        super(context);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.pickcarddialog);
        mHintTextView = (TextView)findViewById(R.id.TextViewPickCardHint);
        mCardPicker = (CardPickerView)findViewById(R.id.PickCardView);
        mCardPicker2 = (CardPickerView)findViewById(R.id.PickCardView2);
        mCardPicker3 = (CardPickerView)findViewById(R.id.PickCardView3);
        mCardPicker.setSelectListener(this);
        mCardPicker2.setSelectListener(this);
        mCardPicker3.setSelectListener(this);
        setCancelable(false);
    }
    public PickCardDialog(Context context, boolean cancelable)
    {
        this(context);
        setCancelable(cancelable);
    }
    
    @Override
    protected void onStop()
    {
        super.onStop();
        if((bGotCard==false)&&(mListener!=null))
            mListener.onSelectCard(null);
    }
    
    public PickCardDialog setCardList(CardList c)
    {
        mCardPicker.setCardList(c);
        mCardPicker.setVisibility(View.VISIBLE);
        return this;
    }
    
    public PickCardDialog setCardList2(CardList c)
    {
        mCardPicker2.setCardList(c);
        mCardPicker2.setVisibility(View.VISIBLE);
        return this;
    }
    
    public PickCardDialog setCardList3(CardList c)
    {
        mCardPicker3.setCardList(c);
        mCardPicker3.setVisibility(View.VISIBLE);
        return this;
    }
    
    public PickCardDialog setCardss(ArrayList<CardList> cardss)
    {
    	mCardPicker.setVisibility(View.GONE);
    	mCardPicker2.setVisibility(View.GONE);
    	mCardPicker3.setVisibility(View.GONE);
    	CardPickerView cpv[] = {mCardPicker, mCardPicker2, mCardPicker3};
    	int row = 0;
    	
    	for(int i = 0;i < cardss.size() && row < cpv.length;i++)
    	{
    		CardList cards = cardss.get(i);
    		if((cards != null) && (cards.size() != 0))
    		{
    			if(row == 0)
        			setCardList(cardss.get(i));
        		else if(row == 1)
        			setCardList2(cardss.get(i));
        		else if(row == 2)
        			setCardList3(cardss.get(i));
        		else
        			break;
    			row++;
    		}
    	}
        return this;
    }
  
    public PickCardDialog setHintText(CharSequence text)
    {
        mHintTextView.setText(text);
        return this;
    }
    
    public PickCardDialog setCardSelectListener(onSelectCardListener listener)
    {
        mListener = listener;
        return this;
    }

    
    public void onSelectCard(Card card)
    {
        bGotCard = true;
        if(mListener!=null)
            mListener.onSelectCard(card);
        dismiss();
    }
    
    

}

